#include "flash.h"

flashData_t flashData;
flashData_t *pFlashData = &flashData;

static void initData(void);

//从0x4000开始存
void initFlash(void)
{
    FLASH_DeInit();
    FLASH_SetProgrammingTime(FLASH_PROGRAMTIME_STANDARD);
    
    if(0 == (*(PointerAttr uint8_t *)0x4000))
    {
        initData();
        saveData();
    }
    else
    {
        readData();
    }
        
}

void initData(void)
{
    u8 t_data[5] = {12, 23, 34, 45, 56};
    memcpy(pFlashData->address, t_data, sizeof(pFlashData->address));
}

void saveData(void)
{
    u8 length = sizeof(flashData);
    u8 *pData = (u8 *)pFlashData;
    FLASH_Unlock(FLASH_MEMTYPE_DATA);
    for(u8 i = 0; i < length; i++)
    {        
        FLASH_ProgramByte(0x4000+i, *(pData+i));
    }
    FLASH_Lock(FLASH_MEMTYPE_DATA);
}

void readData(void)
{
    u8 length = sizeof(flashData);
    u8 *pData = (u8 *)pFlashData;
    for(u8 i = 0; i < length; i++)
    {
        *(pData+i) = (*(PointerAttr uint8_t *)(0x4000 + i));
    }
}

